package com.nl.controller.web.service;

import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.LoginMapper;
import com.nl.controller.web.mapper.ResetPasswordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ResetPassWordService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private ResetPasswordMapper resetPasswordMapper;

    @Autowired
    private LoginMapper loginMapper;

    @SystemServiceLog(description = "检验用户是否存在", value = false)
    public String judgeMsisdn(String msisdn) {
        String result = null;
        try {
            result = resetPasswordMapper.judgeMsisdn(msisdn);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("检验用户是否存在异常：" + msisdn);
            throw new SmsException("检验用户是否存在异常：" + msisdn);
        }
        return result;
    }


    @SystemServiceLog(description = "修改密码", value = false)
    public int resetPassword(String msisdn, String password) {
        int result = 0;
        SmsUsers users = loginMapper.login(msisdn).get(0);
        users.setPassword(password);
        try {
            result = resetPasswordMapper.resetPassword(users);
            commonService.createSmsOperLog(users, "1", "重置密码", "0", "重置密码");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog(users, "1", "重置密码", "1", "重置密码");
            log.error("修改密码异常：" + msisdn);
            throw new SmsException("修改密码异常：" + msisdn);
        }
        return result;
    }

}
